﻿using DevExpress.Utils.Behaviors;
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace APPWG4806.View
{
    public partial class BarQRCodeForm : DevExpress.XtraEditors.XtraForm
    {
        public static Bitmap barCodeBit;

        PrintDocument printDocument;

        Image image;

        public BarQRCodeForm(Bitmap bitmap)
        {
            barCodeBit = bitmap;
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;

            printDocument = new PrintDocument();
            image = barCodeBit;
            printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
        }

        private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            // 计算图片的缩放比例，以适应打印页
            float scaleX = e.MarginBounds.Width / image.Width;
            float scaleY = e.MarginBounds.Height / image.Height;
            float scale = Math.Min(scaleX, scaleY); // 选择较小的缩放比例

            // 计算图片的绘制位置
            RectangleF layoutRect = new RectangleF(
                e.MarginBounds.Left,
                e.MarginBounds.Top,
                image.Width * scale,
                image.Height * scale);

            // 绘制图片
            e.Graphics.DrawImage(image, layoutRect);
        }

        private void BarQRCodeForm_Load(object sender, EventArgs e)
        {
            // 保存二维码
            //barCodeBit.Save("qr_code.png", System.Drawing.Imaging.ImageFormat.Png);
            this.pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
            // 显示二维码
            pictureBox1.Image = barCodeBit;
        }

        private void print_BT_Click(object sender, EventArgs e)
        {
            // 显示是否打印画面
            PrintDialog printDialog = new PrintDialog();
            printDialog.Document = printDocument;

            if (printDialog.ShowDialog() == DialogResult.OK)
            {
                printDocument.Print();
            }
        }

        private void BarQRCodeForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            barCodeBit.Dispose();
        }
    }
}